package com.ss.android.lockscreen.views;

import android.content.Context;
import android.os.Build;
import android.support.v4.view.KeyEventCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.AttributeSet;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import java.util.ArrayList;

public class SwipeView extends RelativeLayout {
    private static final Interpolator a;
    private View b;
    private int c;
    private int d;
    private Scroller e;
    private boolean f;
    private boolean g;
    private boolean h;
    private boolean i;
    private int j;
    private float k;
    private float l;
    private float m;
    private float n;
    private float o;
    private int p;
    private VelocityTracker q;
    private int r;
    private d s;
    private e t;
    private int u;
    private boolean v;
    private boolean w;

    static {
        a = new OvershootInterpolator(1.5f);
    }

    public SwipeView(Context arg2) {
        this(arg2, null);
    }

    public SwipeView(Context arg4, AttributeSet arg5) {
        super(arg4, arg5);
        this.d = 1;
        this.p = -1;
        new ArrayList();
        this.w = false;
        this.setWillNotDraw(true);
        this.setDescendantFocusability(262144);
        this.setFocusable(true);
        Context v0 = this.getContext();
        this.e = new Scroller(v0, SwipeView.a);
        ViewConfiguration v0_1 = ViewConfiguration.get(v0);
        this.j = 50;
        this.r = v0_1.getScaledMaximumFlingVelocity();
        this.b = ((View) this);
        this.s = new d(this);
    }

    public final void a() {
        this.a(1, true);
        if (this.b != null) {
            this.b.setAlpha(1f);
        }
    }

    private int a(int arg4) {
        int v0;
        switch (arg4) {
            case 1: {
                return this.b.getLeft();
            }
            case 0:
            case 2: {
                View v0_1 = this.b;
                if (this.c == 0) {
                    switch (arg4) {
                        case 0: {
                            return v0_1.getLeft() - this.b.getWidth();
                        }
                        case 2: {
                            return v0_1.getLeft();
                        }
                        default: {
                        }
                    }
                } else {
                    if (this.c == 1) {
                        switch (arg4) {
                            case 0: {
                                return v0_1.getLeft();
                            }
                            case 2: {
                                return v0_1.getLeft() + this.b.getWidth();

                            }
                            default: {
                                return v0_1.getLeft();
                            }
                        }
                    } else {
                        if (this.c == 2) {
                            switch (arg4) {
                                case 0:
                                    return v0_1.getLeft() - this.b.getWidth();
                                case 2:
                                    return v0_1.getLeft() + this.b.getWidth();
                                default: {
                                    return v0_1.getLeft();
                                }
                            }
                        }
                        return v0_1.getLeft();
                    }
                }

                return v0_1.getLeft();
            }
            default: {
                v0 = 0;
                break;
            }
        }

        return v0;
    }

    private int a(MotionEvent arg3, int arg4) {
        int v1 = -1;
        int v0 = MotionEventCompat.findPointerIndex(arg3, arg4);
        if (v0 == v1) {
            this.p = v1;
        }

        return v0;
    }

    static View a(SwipeView arg1) {
        return arg1.b;
    }

    private void a(int arg13, boolean arg14) {
        if ((arg14) || this.d != arg13) {
            if (arg13 > 1) {
                arg13 = 2;
            } else if (arg13 <= 0) {
                arg13 = 0;
            }

            if (this.c == 0 && arg13 > 1) {
                arg13 = 0;
            } else if (this.c == 1 && arg13 <= 0) {
                arg13 = 2;
            }

            boolean v0 = this.d != arg13 ? true : false;
            this.h = v0;
            this.d = arg13;
            int v0_1 = this.a(this.d);
            int v1 = this.getScrollX();
            int v2 = this.getScrollY();
            int v3 = v0_1 - v1;
            int v4 = -v2;
            if (v3 == 0 && v4 == 0) {
                this.b();
                return;
            }

            this.setScrollingCacheEnabled(true);
            this.g = true;
            v0_1 = this.b.getWidth();
            int v5 = v0_1 / 2;
            float v0_2 = (((float) Math.sin(((double) (((float) ((((double) (Math.min(1f, 1f * (((float) Math.abs(v3))) / (((float) v0_1))) - 0.5f))) * 0.471239))))))) * (((float) v5)) + (((float) v5));
            v5 = Math.abs(0);
            if (v5 > 0) {
                v0_1 = Math.round(Math.abs(v0_2 / (((float) v5))) * 1000f) * 4;
            } else {
                Math.abs(v3);
                v0_1 = 600;
            }

            this.e.startScroll(v1, v2, v3, v4, Math.min(v0_1, 600));
            this.invalidate();
        } else {
            this.setScrollingCacheEnabled(false);
        }
    }

    private boolean a(MotionEvent arg9) {
        float v4;
        int v6 = -1;
        float v5 = -1f;
        boolean v2 = false;
        if (this.g) {
            return v2;
        }

        if (this.q == null) {
            return v2;
        }

        this.q.addMovement(arg9);
        switch (arg9.getAction() & 255) {
            case 0: {
                this.u = MotionEventCompat.getPointerId(arg9, MotionEventCompat.getActionIndex(arg9));
                if (this.u != v6) {
                    try {
                        this.m = MotionEventCompat.getY(arg9, this.u);
                        this.n = MotionEventCompat.getX(arg9, this.u);
                    } catch (Throwable v0_4) {
                        this.m = v5;
                        this.n = v5;
                    }

                    this.v = false;
                }

                return true;
            }
            case 2: {
                int v0 = this.u;
                int v3 = this.a(arg9, v0);
                if (v0 == v6) {
                    return v2;
                }

                if (v3 == v6) {
                    return v2;
                }

                float v0_1 = MotionEventCompat.getY(arg9, v3);
                float v3_1 = MotionEventCompat.getX(arg9, v3);
                if (this.m == v5) {
                    this.m = v0_1;
                }

                if (this.n == v5) {
                    this.n = v3_1;
                }

                if (!this.v) {
                    v4 = v0_1 - this.m;
                    v5 = Math.abs(v3_1 - this.n);
                    float v6_1 = Math.abs(v4);
                    if (v4 < 0f && v6_1 > (((float) this.j)) && v6_1 >= 0.5f * v5) {
                        this.v = true;
                        this.n = v3_1;
                        this.m = v0_1;
                        this.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                }

                if (this.v) {
                    this.n = v3_1;
                    this.m = v0_1;
                }

                if (!this.v) {
                    return true;
                }

                if (this.getScrollY() != 0) {
                    return true;
                }

                if (this.t == null) {
                    return true;
                }

                if (this.getScrollX() == 0) {
                    this.t.a(((int) (this.o - v0_1)));
                    return true;
                }

                this.t.a(0);
                return true;
            }
            case 1:
            case 3:
            case 6: {
                System.out.println("SwipeView Action_Up_cancel misBeingDraggedVertical " + this.v);
                if (this.v) {
                    VelocityTracker v0_2 = this.q;
                    v0_2.computeCurrentVelocity(1000, ((float) this.r));
                    int v3 = ((int) VelocityTrackerCompat.getYVelocity(v0_2, this.u));
                    int v0 = this.a(arg9, this.u);
                    if (v0 != v6) {
                        v4 = MotionEventCompat.getY(arg9, v0) - this.o;
                        boolean v0_3 = v3 < 0 ? true : false;
                        if (Math.abs(v3) > 600) {
                            if (this.t == null) {
                            } else if (this.getScrollX() != 0) {
                            } else if (this.getScrollX() == 0) {
                                this.t.a(true, v0_3, v4);
                            } else {
                                this.t.a(0);
                            }
                        } else if (this.t == null) {

                        } else if (this.getScrollX() == 0) {
                            this.t.a(false, v0_3, v4);
                        } else {
                            this.t.a(0);
                        }
                    }
                }

                this.v = false;
                this.getParent().requestDisallowInterceptTouchEvent(false);
                if (this.q == null) {
                    return true;
                }

                this.q.recycle();
                this.q = null;
                return true;
            }
        }

        return true;



    }

    private void b() {
        if (this.g) {
            this.setScrollingCacheEnabled(false);
            this.e.abortAnimation();
            int v0 = this.getScrollX();
            int v1 = this.getScrollY();
            int v2 = this.e.getCurrX();
            int v3 = this.e.getCurrY();
            if (v0 == v2 && v1 == v3) {
            } else {
                this.scrollTo(v2, v3);
                float v0_1 = (((float) v2)) / (((float) this.b.getWidth()));
                if (this.s == null) {
                } else {
                    this.s.a(v0_1);
                }
            }
        }

        this.h = false;
        this.g = false;
    }

    private boolean b(int arg7) {
        boolean v0_1;
        int v5 = 66;
        int v4 = 17;
        boolean v1 = true;
        boolean v2 = false;
        View v0 = this.findFocus();
        if ((((SwipeView) v0)) == this) {
            v0 = null;
        }

        View v3 = FocusFinder.getInstance().findNextFocus(((ViewGroup) this), v0, arg7);
        if (v3 == null || v3 == v0) {
            if (arg7 != v4 && arg7 != 1) {
                if (arg7 == v5 || arg7 == 2) {
                    label_40:
                    if (this.d <= 0) {
                        this.a(this.d + 1, false);
                        v2 = v1;
                    } else {
                        v1 = false;
                        v2 = v1;
                    }
                }
            } else {
                if (this.d > 0) {
                    this.a(this.d - 1, false);
                    v0_1 = true;
                } else {
                    v0_1 = false;
                }
                v2 = v0_1;
            }
        } else if (arg7 == v4) {
            v1 = v3.requestFocus();
            v2 = v1;
        } else if (arg7 == v5) {
            if (v0 != null && v3.getLeft() <= v0.getLeft()) {
                if (this.d <= 0) {
                    this.a(this.d + 1, false);
                    v2 = v1;
                } else {
                    v1 = false;
                    v2 = v1;
                }
            } else {
                v1 = v3.requestFocus();
                v2 = v1;
            }
        }
        if (v2) {
            this.playSoundEffect(SoundEffectConstants.getContantForFocusDirection(arg7));
        }

        return v2;
    }

    public void computeScroll() {
        if ((this.e.isFinished()) || !this.e.computeScrollOffset()) {
            this.b();
        } else {
            int v0 = this.getScrollX();
            int v1 = this.getScrollY();
            int v2 = this.e.getCurrX();
            int v3 = this.e.getCurrY();
            if (v0 != v2 || v1 != v3) {
                this.scrollTo(v2, v3);
                v0 = this.getWidth();
                v1 = v2 / v0;
                v0 = v2 % v0;
                if (v1 == 0) {
                    float v0_1 = (((float) v0)) / (((float) this.b.getWidth()));
                    if (this.s != null) {
                        this.s.a(v0_1);
                    }
                } else if (!this.w) {
                    this.w = true;
                }
            }

            this.invalidate();
        }
    }

    public boolean dispatchKeyEvent(KeyEvent arg5) {
        boolean v2;
        boolean v0 = false;
        if (!super.dispatchKeyEvent(arg5)) {
            if (arg5.getAction() == 0) {
                switch (arg5.getKeyCode()) {
                    case 21: {
                        v2 = this.b(17);
                        break;
                    }
                    case 22: {
                        v2 = this.b(66);
                        break;
                    }
                    case 61: {
                        if (Build.VERSION.SDK_INT < 11) {
                            v2 = false;
                        } else if (KeyEventCompat.metaStateHasNoModifiers(arg5.getMetaState())) {
                            v2 = this.b(2);
                        } else if (KeyEventCompat.metaStateHasModifiers(arg5.getMetaState(), 1)) {
                            v2 = this.b(1);
                        } else {
                            v2 = false;
                        }
                        break;
                    }
                    default:
                        v2 = false;
                        break;
                }
            } else {
                v2 = false;
            }
            if (!v2) {
                return v0;
            }
            return true;
        } else {
            v0 = true;
        }

        return v0;
    }

    public View getContent() {
        return this.b;
    }

    public int getContentLeft() {
        return this.b.getLeft() + this.b.getPaddingLeft();
    }

    private int getLeftBound() {
        int v0_1;
        View v0 = this.b;
        if (this.c == 0 || this.c == 2) {
            v0_1 = v0.getLeft() - this.b.getWidth();
        } else if (this.c == 1) {
            v0_1 = v0.getLeft();
        } else {
            v0_1 = 0;
        }

        return v0_1;
    }

    public int getMode() {
        return this.c;
    }

    private int getRightBound() {
        int v0_1;
        View v0 = this.b;
        if (this.c == 0) {
            v0_1 = v0.getLeft();
        } else {
            if (this.c != 1 && this.c != 2) {
                return 0;
            }

            v0_1 = v0.getLeft() + this.b.getWidth();
        }

        return v0_1;
    }

    public boolean onInterceptTouchEvent(MotionEvent arg9) {
        int v2;
        int v4 = -1;
        boolean v0 = true;
        switch (arg9.getAction() & 255) {
            case 0: {
                v2 = MotionEventCompat.getActionIndex(arg9);
                this.p = MotionEventCompat.getPointerId(arg9, v2);
                if (this.p == v4) {
                    break;
                }

                this.k = MotionEventCompat.getX(arg9, v2);
                this.l = MotionEventCompat.getY(arg9, v2);
                float v2_1 = this.l;
                this.o = v2_1;
                this.m = v2_1;
                this.n = this.k;
                v2 = !this.h || !this.g ? 1 : 0;
                if (v2 == 0) {
                    break;
                }

                this.i = false;
                break;
            }
            case 2: {
                v2 = this.p;
                int v3 = this.a(arg9, v2);
                if (v2 == v4) {
                    break;
                }

                if (v3 == v4) {
                    break;
                }

                float v4_1 = MotionEventCompat.getX(arg9, v3);
                float v5 = Math.abs(v4_1 - this.k);
                float v3_1 = MotionEventCompat.getY(arg9, v3);
                float v6 = Math.abs(v3_1 - this.l);
                v2 = this.d == 0 || this.d == 2 ? 1 : 0;
                v2 = v2 != 0 ? this.j / 2 : this.j;
                if (v6 <= (((float) v2))) {
                    break;
                }

                if (v6 < 0.5f * v5) {
                    break;
                }

                this.i = true;
                this.w = false;
                this.getParent().requestDisallowInterceptTouchEvent(true);
                this.k = v4_1;
                this.l = v3_1;
                this.setScrollingCacheEnabled(true);
                break;
            }
        }
        if (!this.i) {
            if (this.q == null) {
                this.q = VelocityTracker.obtain();
            }

            this.q.addMovement(arg9);
        }

        if (!this.i) {
            v0 = false;
        }

        return v0;
    }

    protected void onSizeChanged(int arg3, int arg4, int arg5, int arg6) {
        super.onSizeChanged(arg3, arg4, arg5, arg6);
        if (arg3 != arg5) {
            this.b();
            this.scrollTo(this.a(this.d), this.getScrollY());
        }
    }

    public boolean onTouchEvent(MotionEvent arg2) {
        return this.a(arg2);
    }

    public void scrollTo(int arg1, int arg2) {
        super.scrollTo(arg1, arg2);
    }

    public void setContent(View arg2) {
        if (this.b != null) {
            this.removeView(this.b);
        }

        this.b = arg2;
        this.addView(this.b);
    }

    public void setMode(int arg1) {
        this.c = arg1;
    }

    private void setScrollingCacheEnabled(boolean arg2) {
        if (this.f != arg2) {
            this.f = arg2;
        }
    }

    public void setSwipeViewVerticalListener(e arg1) {
        this.t = arg1;
    }
}

